#include "Menu.h"
#include <SFML\Window.hpp>
#include <iostream>

Menu::Menu(sf::RenderWindow* window, StateMachine* state_machine) : _window(window), GameState(state_machine)
{


	sf::Text play;
	sf::Text quit;
	play.setString(L"Play");
	quit.setString(L"Quit");

	int x= window->getSize().x /2;

	play.setColor(sf::Color::Red);
	play.setPosition(x, window->getSize().y /3);
	quit.setPosition(x, window->getSize().y * 2/3);

	if (!_font.loadFromFile("police.ttf"))
		std::cerr<<"Error : police not found"<<std::endl;
	else
	{
		play.setFont(_font);
		quit.setFont(_font);
	}

	play.setOrigin(play.getLocalBounds().width/2,0);
	quit.setOrigin(play.getLocalBounds().width/2,0);

	myTexts.push_back(play);
	myTexts.push_back(quit);
}

void Menu::update(){
	_window->clear();
	draw();
	handleEvents();
}

void Menu::handleEvents()
{
	sf::Event event;
    while(_window->pollEvent(event))
    {
        if(event.type == sf::Event::Closed)
            _window->close();
    }

	if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
	{
		sf::Vector2i mouse_pos = sf::Mouse::getPosition(*_window);
		if( myTexts[0].getGlobalBounds().contains(mouse_pos.x,mouse_pos.y))
		{
			_state_machine->changeState(state::game);
		}
		else if( myTexts[1].getGlobalBounds().contains(mouse_pos.x,mouse_pos.y))
		{
			_state_machine->quit();
		}
	}
}

void Menu::exit(){}

void Menu::draw()
{
	for(int i=0; i<myTexts.size(); i++)
	{
		_window->draw(myTexts[i]);
	}
	
}